/**
 * @param {number} n
 * @param {number[][]} edges
 * @param {number} time
 * @param {number} change
 * @return {number}
 */
 var secondMinimum = function(n, edges, time, change) {
  const g = new Map()
  for(const [x, y] of edges){
      if(!g.has(x)) g.set(x, [])
      if(!g.has(y)) g.set(y, [])
      g.get(x).push(y)
      g.get(y).push(x)
  }
  let costs = []
  const used = new Array(n).fill(false)
  const dfs = (s, e, cost) => {
      if(s === e){
          costs.push(cost)
          return
      }
      for(const next of g.get(s)){
          if(used[next]) continue
          used[next] = true
          let x = (cost+time) / change  >> 0
          if(x % 2 === 0){
              dfs(next, e, cost + time)
          }else{
              dfs(next, e, cost + time + (change-x))
          }
          used[next] = false
      }
  }
  dfs(1, n, 0)
  console.log(costs)
  costs = [...new Set(costs)]
  costs.sort((a,b) => a-b)
  if(costs.length > 1){
      return costs[1]
  }
  const m = (costs[0]*2) / change >> 1
  return costs[0]*2 + change * m - 1
};